package calbs.socket.jms.consumer.trace;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.MessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.jms.support.converter.SimpleMessageConverter;

import javax.annotation.Resource;
import javax.jms.Queue;

/**
 * 轨迹消息队列定义
 * 张钦
 * 20170807
 */
@Configuration
@Description(value = "轨迹队列")
public class TraceConfig {
	@Resource
	private Queue zdjsLocationQueue;
	@Bean(name="textMessageListenerAdapter")
	public MessageListenerAdapter messageListenerAdapter() {
		MessageListenerAdapter adapter = new MessageListenerAdapter();
		adapter.setMessageConverter(new SimpleMessageConverter());
		adapter.setDelegate(textConsumerListener());
		return adapter;
	}

	public CachingConnectionFactory connectionFactory(ActiveMQConnectionFactory connectionFactory) {
		CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory);
		return factory;
	}

	@Bean
	public MessageListenerContainer messageListenerContainer(@Qualifier("jmsConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
		container.setConnectionFactory(connectionFactory(connectionFactory));
		container.setDestination(zdjsLocationQueue);
		container.setMessageListener(messageListenerAdapter());
		container.setConcurrency("10-50");
		return container;
	}

	@Bean
	public TraceListener textConsumerListener() {
		return new TraceListener();
	}
}